package pers.youqu.page.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import pers.youqu.common.utils.JsonUtils;
import pers.youqu.item.clients.ItemClient;
import pers.youqu.item.dto.*;
import pers.youqu.page.dto.SpecGroupNameDTO;
import pers.youqu.page.dto.SpecParamNameDTO;
import pers.youqu.page.service.GoodsPageService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/15 17:43
 * @Description TODO
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String REDIS_KEY_PREFIX_SKU = "page:sku:id:";

    @Override
    public String loadSpuData(Long spuId) {

        SpuDTO spuDTO = itemClient.findSpuBySpuId(spuId);
        // 页面需要数据的封装
        Map<String, Object> map = new HashMap<>();
        map.put("id", spuDTO.getId());
        map.put("name", spuDTO.getName());
        map.put("categoryIds", spuDTO.getCategoryIds());
        map.put("brandId", spuDTO.getBrandId());

        // 转换成json字符串存储到redis中
        String json = JsonUtils.toJson(map);

        redisTemplate.opsForValue().set("page:spu:id:" + spuId, json);

        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {

        List<SkuDTO> skuDTOS = itemClient.findSkuBySpuId(spuId);

        String json = JsonUtils.toJson(skuDTOS);

        redisTemplate.opsForValue().set(REDIS_KEY_PREFIX_SKU + spuId, json);

        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        SpuDetailDTO spuDetailDTO = itemClient.findSpuDetailBySpuId(spuId);

        String json = JsonUtils.toJson(spuDetailDTO);

        redisTemplate.opsForValue().set("page:detail:id:" + spuId, json);

        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        // 查询信息
        List<CategoryDTO> list = itemClient.findCategoryByListId(ids);
        List<Map<String, Object>> categoryList = list.stream().map(categoryDTO -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            return map;
        }).collect(Collectors.toList());
        String json = JsonUtils.toJson(categoryList);
        // 存入Redis
        redisTemplate.opsForValue().set("page:category:id:" + ids.get(2), json);
        return json;
    }

    @Override
    public String loadBrandData(Long id) {
        BrandDTO brandDTO = itemClient.findBrandById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("id", brandDTO.getId());
        map.put("name", brandDTO.getName());

        String json = JsonUtils.toJson(map);

        redisTemplate.opsForValue().set("page:brand:id:" + id, json);

        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {

        List<SpecGroupDTO> specGroupDTOS = itemClient.findSpecGroupListByCid(categoryId);

        List<SpecGroupNameDTO> specGroupNameDTOS = specGroupDTOS.stream().map(specGroupDTO -> {
            SpecGroupNameDTO specGroupNameDTO = new SpecGroupNameDTO();
            specGroupNameDTO.setName(specGroupDTO.getName());
            specGroupNameDTO.setParams(specGroupDTO.getParams()
                    .stream()
                    .map(specParamDTO -> specParamDTO.toEntity(SpecParamNameDTO.class))
                    .collect(Collectors.toList()));

            return specGroupNameDTO;
        }).collect(Collectors.toList());

        String json = JsonUtils.toJson(specGroupNameDTOS);

        redisTemplate.opsForValue().set("page:spec:id:" + categoryId, json);

        return json;
    }

    /**
     * sku修改将sku从redis中删除
     * @param spuId
     * @return
     */
    @Override
    public Boolean deleteSku(Long spuId) {
        return redisTemplate.delete(REDIS_KEY_PREFIX_SKU + spuId);
    }


}
